package middleware

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"vueAdmin/application/model"
	"vueAdmin/application/utils"
)

// 验证token
func CheckToken() gin.HandlerFunc {
	return func(c *gin.Context) {
		defer utils.ErrHandle(c)
		token := c.Request.Header.Get("Authorization")
		id, err := model.RedisClient.Get(token).Result()
		//fmt.Println(token)
		//fmt.Println(id)
		if err == nil && id != "" {
			c.Set("user_id", id)
			c.Next()
		} else {
			c.JSON(http.StatusOK, utils.SetPackageData(1004, nil))
			c.Abort()
			return
		}

	}
}
